package com.jixi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jixi.common.result.Result;
import com.jixi.mapper.*;
import com.jixi.pojo.entity.*;
import com.jixi.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class StatisticsServiceImpl implements StatisticsService {
    
    @Autowired
    private OrderDailyMapper orderDailyMapper;
    
    @Autowired
    private ReturnOrderDetailMapper returnOrderDetailMapper;
    
    @Autowired
    private CompanyMapper companyMapper;
    
    @Autowired
    private SchoolMapper schoolMapper;
    
    @Autowired
    private ClassesMapper classesMapper;
    
    @Autowired
    private TeacherMapper teacherMapper;
    
    @Autowired
    private StudentMapper studentMapper;
    
    @Autowired
    private SetmealMapper setmealMapper;
    
    /**
     * 9项统计信息
     *
     * @return 9项统计信息
     */
    @Override
    public Result<Map<String, Long>> getStatistics() {
        Map<String, Long> resultMap = new HashMap<>();
        //订餐数
        LambdaQueryWrapper<OrderDaily> queryWrapperOrder = new LambdaQueryWrapper<>();
        queryWrapperOrder.eq(OrderDaily::getIsDeleted, 0);
        //仅统计未完成和已完成的
        queryWrapperOrder.in(OrderDaily::getOrderDateStatus, 0, 1);
        resultMap.put("orderNum", orderDailyMapper.selectCount(queryWrapperOrder));

        //退餐数
        LambdaQueryWrapper<ReturnOrderDetail> queryWrapperReturn = new LambdaQueryWrapper<>();
        queryWrapperReturn.eq(ReturnOrderDetail::getIsDeleted, 0);
        //统计待退款和已退款的
        resultMap.put("returnNum", returnOrderDetailMapper.selectCount(queryWrapperReturn));

        //餐饮公司数
        LambdaQueryWrapper<Company> queryWrapperCompany = new LambdaQueryWrapper<>();
        queryWrapperCompany.eq(Company::getIsDeleted, 0);
        //仅统计启用的
        queryWrapperCompany.eq(Company::getStatus, 1);
        resultMap.put("companyNum", companyMapper.selectCount(queryWrapperCompany));

        //学校数
        LambdaQueryWrapper<School> queryWrapperSchool = new LambdaQueryWrapper<>();
        queryWrapperSchool.eq(School::getIsDeleted, 0);
        //仅统计启用的
        queryWrapperSchool.eq(School::getStatus, 1);
        resultMap.put("schoolNum", schoolMapper.selectCount(queryWrapperSchool));

        //班级数
        LambdaQueryWrapper<Classes> queryWrapperClass = new LambdaQueryWrapper<>();
        queryWrapperClass.eq(Classes::getIsDeleted, 0);
        //仅统计启用的
        queryWrapperClass.eq(Classes::getStatus, 1);
        resultMap.put("classNum", classesMapper.selectCount(queryWrapperClass));

        //班主任数
        LambdaQueryWrapper<Teacher> queryWrapperTeacher = new LambdaQueryWrapper<>();
        queryWrapperTeacher.eq(Teacher::getIsDeleted, 0);
        //仅统计启用的
        queryWrapperTeacher.eq(Teacher::getStatus, 1);
        resultMap.put("teacherNum", teacherMapper.selectCount(queryWrapperTeacher));

        //学生数
        LambdaQueryWrapper<Student> queryWrapperStudent = new LambdaQueryWrapper<>();
        queryWrapperStudent.eq(Student::getIsDeleted, 0);
        //仅统计启用的
        queryWrapperStudent.eq(Student::getStatus, 1);
        resultMap.put("studentNum", studentMapper.selectCount(queryWrapperStudent));

        //待审核的套餐数量
        LambdaQueryWrapper<Setmeal> queryWrapperSetmeal = new LambdaQueryWrapper<>();
        queryWrapperSetmeal.eq(Setmeal::getIsDeleted, 0);
        //仅统计待审核的套餐
        queryWrapperSetmeal.eq(Setmeal::getAuditStatus, 2);
        resultMap.put("examineSetmealNum", setmealMapper.selectCount(queryWrapperSetmeal));

        //退餐待退款数量
        LambdaQueryWrapper<ReturnOrderDetail> queryWrapperReturnOrderDetail = new LambdaQueryWrapper<>();
        queryWrapperReturnOrderDetail.eq(ReturnOrderDetail::getIsDeleted, 0);
        //仅统计待退款和退款失败
        queryWrapperReturnOrderDetail.in(ReturnOrderDetail::getReturnStatus, 0, 2);
        resultMap.put("examineReturnNum", returnOrderDetailMapper.selectCount(queryWrapperReturnOrderDetail));

        return Result.success(resultMap);
    }
}
